package cn.dapeng.tree.binary;

/**
 * 判断两颗树，是否为相同的树
 * <p>
 * 对于每一颗自述来说，头结点==头节点 左节点==左节点 右节点==右节点
 */
public class CheckSameTree {

    public static boolean isSameTree(TreeNode a, TreeNode b) {

        if (a == null && b == null) {
            return true;
        }
        if (a == null ^ b == null) {
            return false;
        }
        return a.val.equals(b.val) && isSameTree(a.left, b.left) && isSameTree(a.right, b.right);
    }


    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println(false ^ false);
        System.out.println(false ^ true);
        System.out.println(true ^ true);
        System.out.println(" =================== ");
        TreeNode head = new TreeNode(0);
        TreeNode l1 = new TreeNode(1);
        TreeNode r1 = new TreeNode(2);

        head.left = l1;
        head.right = r1;

        head.left.left = new TreeNode(5);
        head.left.left.left = new TreeNode(7);

        TreeNode h2 = head.clone();

        h2.left.left = new TreeNode(5);
        h2.left.left.left = new TreeNode(7);

        System.out.println(head == h2);
        System.out.println(head.left == h2.left);
        System.out.println(head.right == h2.right);

        System.out.println(" ================= ");

        System.out.println(isSameTree(h2, head));
    }

}
